import { _decorator, Color, EventTouch, find, Label, Sprite, Node } from 'cc';
import { ConfigManager } from '../Common/Runtime/ConfigManager';
import { EntityTypeEnum, SelectPoolTypeEnum, toFixed } from '../Common';
import { SelectManager } from '../Common/Runtime/SelectManager';
import { UIComponent } from '../Base/UIComponent';
import DataManager from '../Runtime/DataManager';
import { getEquipAttrLabel, getIcon, getPropAttrLabel, getSkillDescLabel } from '../Runtime/MainManager';
import { getPanelBaseColor, getPanelFrameColor } from '../Utils';
import { TalentManager } from '../Runtime/TalentManager';
const { ccclass, property } = _decorator;

@ccclass('SelectUI')
export class SelectUI extends UIComponent {

    refreshButton: Node
    protected onLoad(): void {

        this.refreshButton = this.node.getChildByName('refreshButton')

    }

    show() {
        this.setMaskEffect()
        if (DataManager.Ins.state.playerLevel < 6) {
            SelectManager.Ins.setSelectWeaponPool()
        } else {
            SelectManager.Ins.setSelectPool()
        }
        this.node.active = true
        this.refreshSelect()
        this.refreshButton.active = TalentManager.Ins.getRefreshSelect()


    }

    setPanel(i: number) {
        const node = find('Layout/select' + (i + 1), this.node)
        const frame = node.getChildByName('frame').getComponent(Sprite)
        const panel = node.getChildByName('panel').getComponent(Sprite)
        const icon = node.getChildByName('icon')
        const content = node.getChildByName('content')
        content.destroyAllChildren()
        const lab_name = node.getChildByName('lab_name').getComponent(Label)
        const lab_type = node.getChildByName('lab_type').getComponent(Label)

        const pool = SelectManager.Ins.pool[i]
        if (pool) {
            node.active = true
            const { id, star } = pool
            frame.color = new Color().fromHEX(getPanelFrameColor(star))
            panel.color = new Color().fromHEX(getPanelBaseColor(star))

            const poolCfg = ConfigManager.Ins.getSelectPoolCfg(id)
            const entity = ConfigManager.Ins.getEntityCfg(poolCfg.entityType)
            getIcon({ entityType: entity.entityType, star }).then((node) => {
                node.parent = icon
            })

            lab_name.string = entity.name

            if (poolCfg.selectType === SelectPoolTypeEnum.weapon) {
                lab_type.string = '武器'
                const weapon = ConfigManager.Ins.getWeaponCfg({ entityType: entity.entityType, star })

                let damageLab = '' + weapon.damage
                if (weapon.entityType === EntityTypeEnum.weapon1 || weapon.entityType === EntityTypeEnum.weapon3) {
                    damageLab = `${weapon.damage}(x${weapon.special})`
                }

                getEquipAttrLabel('伤害', damageLab, content)
                getEquipAttrLabel('范围', weapon.aimRadiu, content)
                getEquipAttrLabel('冷却', weapon.shootCD, content)
                getEquipAttrLabel('贯穿', weapon.penetrate, content)
                getEquipAttrLabel('暴击', `x${weapon.critRate}(${toFixed(weapon.critChance)}%概率)`, content)

            } else if (poolCfg.selectType === SelectPoolTypeEnum.prop) {
                lab_type.string = '道具'
                const prop = ConfigManager.Ins.getPropCfg({ entityType: entity.entityType, star })

                getPropAttrLabel(prop, content)
            } else if (poolCfg.selectType === SelectPoolTypeEnum.skill) {
                lab_type.string = '技能'
                const skillCfg = ConfigManager.Ins.getSkillCfg(entity.entityType)
                getSkillDescLabel(skillCfg, content)
            }

            if (poolCfg.selectType === SelectPoolTypeEnum.skill) {
                lab_type.string = '获得' + lab_type.string
            } else if (star > 1) {
                lab_type.string = lab_type.string + '升级'
            } else {
                lab_type.string = '获得' + lab_type.string
            }

        } else {
            node.active = false
            content.destroyAllChildren()
        }
    }

    onClick(e: EventTouch, arg: 0 | 1 | 2) {
        SelectManager.Ins.doSelect(arg)

        if (DataManager.Ins.state.actors[0].skillPoint > 0) {
            if (DataManager.Ins.selectPool.length > 0) {
                this.show()
                return
            } else {
                console.log('没有可选项了')
                DataManager.Ins.state.actors[0].skillPoint = 0
            }
        }

        this.node.active = false
        DataManager.Ins.state.isPause = 0
    }

    /** 刷新选项 */
    refreshSelect() {
        SelectManager.Ins.shuffle()
        for (let i = 0; i < 3; i++) {
            this.setPanel(i)
        }
    }

    onRefreshSelect() {
        this.refreshButton.active = false
        this.refreshSelect()
    }

}


